﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace SES.CMS
{
    public class ShopingCart
    {
        //Lam thu gio hang voi datatable
        DataTable tb = new DataTable();

        public ShopingCart()
        {
            //if (tb.Columns == null)
            {
                tb.Columns.Add("STT");
                tb.Columns.Add("SanPhamID");
                tb.Columns.Add("SoLuong");
                tb.Columns.Add("ThanhTien");
            }
        }

        public DataTable getTable()
        {
            return this.tb;
        }

        public void setTable(DataTable tb)
        {
            this.tb = tb;
        }

        public DataTable init(long SanPhamID, int SoLuong, float ThanhTien)
        {
            DataRow row = tb.NewRow();
            row[0] = 1;
            row[1] = SanPhamID;
            row[2] = SoLuong;
            row[3] = ThanhTien;
            tb.Rows.Add(row);
            return tb;
        }
        //Cap nhat so luon
        //phuong thuc them 1 san pham moi vao gioi hang
        public DataTable addSP(long SanPhamID, int SoLuong, float ThanhTien)
        {
            DataRow row = tb.NewRow();
            row[0] = tb.Rows.Count + 1;
            row[1] = SanPhamID;
            row[2] = SoLuong;
            row[3] = ThanhTien;
            tb.Rows.Add(row);
            return tb;
        }

        public DataTable delete(DataTable datatable, long SanPhamID)
        {
            DataTable tb = datatable;
            foreach (DataRow row in tb.Rows)
            {
                if (int.Parse(row[1].ToString()) == SanPhamID)
                {
                    tb.Rows.Remove(row);
                    break;
                }
            }
            return tb;
        }

        public DataTable upDate(DataTable datatable, long SanPhamID, int SoLuong, float gia)
        {
            DataTable tb = datatable;
            foreach (DataRow row in tb.Rows)
            {
                if (int.Parse(row[1].ToString()) == SanPhamID)
                {
                    row[2] = int.Parse(row[2].ToString()) + SoLuong;
                    row[3] = gia * int.Parse(row[2].ToString());
                }
            }
            return tb;
        }

        public DataTable upDateSL(DataTable datatable, int STT, int SoLuong, decimal ThanhTien)
        {
            DataTable tb = datatable;
            foreach (DataRow row in tb.Rows)
            {
                if (int.Parse(row[0].ToString()) == STT)
                {
                    row[2] = SoLuong;
                    row[3] = ThanhTien;
                }
            }
            return tb;
        }
    }
}
